<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css"
        integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">


    <link href="css/base.css" rel="stylesheet">
    <link href="./css/head.css" rel="stylesheet">
    <link href="./admin/css/carousel.css" rel="stylesheet">
    <link href="./css/index.css" rel="stylesheet">
</head>
<body>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"
        integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
        crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"
        integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN"
        crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js"></script>

    <script src="js/jquery-3.6.0.min.js"></script>
    <script src="js/Utils.js"></script>
    <script src="js/head.js"></script>

    <div class="head"></div>

    <div class="nav_head"></div>

    <div class="main">
        <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
            <ol class="carousel-indicators"></ol>
            <div class="carousel-inner"></div>
            <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
                <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                <span class="sr-only">Previous</span>
            </a>
            <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
                <span class="carousel-control-next-icon" aria-hidden="true"></span>
                <span class="sr-only">Next</span>
            </a>
        </div>

        <div class="typeBox">

        </div>
    </div>

    <script>
        function gotoPets(data){
            setSession('type', data)
            location.href="pets.html"
        }

        function gotoPet(id,type){
            setSession('type', type)

            location.href="pet.html?id="+id
        }

        function renderCarousel(data){
            
            for (let i = 0; i < data.length; i++) {
                
                $('.main .carousel-indicators').append(`
                    <li data-target="#carouselExampleIndicators" data-slide-to="${i}"></li>
                `)

                $('.main .carousel-inner').append(`
                    <div class="carousel-item">
                        <img src="${baseUrl + data[i].image}" class="d-block w-100" alt="...">
                    </div>
                `)
                        
                $('.main .carouselBox').append(`
                    <div class="carousel">
                        <div class="carouselImg">
                            <img src="${baseUrl + data[i].image}">
                        </div>

                        <div class="operationBox">
                            <div class="button" onclick="delCarousel('${data[i].id}')">删除</div>
                        </div>    
                    </div>
                `)
            }

            $('.main .carousel-indicators').children().eq(0).addClass('active')
            $('.main .carousel-inner').children().eq(0).addClass('active')
        }

        function renderTypesPets(data){
            for(d of data){
                
                $(".typeBox").append(`
                    <div>
                        <div class="type">
                            
                                <img src="${baseUrl + d.image}" />
                            

                            <div>
                                <a href="#" onclick=gotoPets('${d.type}') style="float:right;">更多</a>    
                            </div>
                        </div>
                        <div class="petBox"></div>
                    </div>
                `)

                for(pet of d['pet_list']){
                    
                    if(!pet.name)   return
                    $(".typeBox .petBox:last").append(`
                        <div class="pet" onclick="gotoPet('${pet.id}', '${pet.type}')">
                            <div class="petImage">
                                <img src="${baseUrl + pet.image}"  />
                            </div>
                            <div class="name">${pet.name}</div>
                        </div>
                    `)
                }
            }
        }

        $("document").ready(function(){
            $.ajax({
                url : baseUrl + '/carousel.php',
                type : 'GET',
                success:function(res){
                    if(typeof(res) == 'string')
                        res = JSON.parse(res)
                    renderCarousel(res.data);
                }
            })

            $.ajax({
                url : baseUrl + '/types_pets.php',
                type : 'GET',
                success: function(res){
                    if(typeof(res) == 'string')
                        res = JSON.parse(res)
                    
                    if(res.code == 200)
                        renderTypesPets(res.data)
                    else
                        alert(res.msg)
                }
            })
        })
    </script>
</body>
</html>